# -*- coding: utf-8 -*-
import mimetypes, os.path
from zope.component import getMultiAdapter
from z3c.pt.pagetemplate import ViewPageTemplateFile

from pargo.utils.view import View, PageTemplateView


class FileDetailsView(PageTemplateView):
    """
    View details of file.
    """
    template = ViewPageTemplateFile('templates/file.pt')


class FileDownloadView(View):
    """
    Download file.
    """
    
    def render(self):
        """
        Attach file content to response.
        """
        filename = self.context.getName()
        mimetype, encoding = mimetypes.guess_type(filename)
        if not mimetype:
            mimetype = 'application/octet-stream'
        
        response = self.request.response  
        response.content_type = mimetype
        response.headers['Content-Disposition'] = 'attachment; filename="%s"' % filename
        return self.context.file


class ImageDetailsView(PageTemplateView):
    """
    View details of file.
    """
    template = ViewPageTemplateFile('templates/image.pt')
